/**
 * @file index.js
 * @author swan
 */
const app = getApp()
var Api = require('../../utils/api.js');
var util = require('../../utils/util.js');
var bdApi = require('../../utils/bdApi.js');
var bdRequest = require('../../utils/bdRequest.js')
import config from '../../utils/config.js'
var pageCount = config.getPageCount;
var WxParse = require('../../wxParse/wxParse.js');
import seoConfig from '../../seoConfig/config.js'
Page({
    data: {
        title: '在线问答',
        postsList: [],
        pagesList: {},
        categoriesList: {},
        postsShowSwiperList: {},
        isLastPage: false,
        page: 1,
        search: '',
        categories: 0,
        categoriesName:'档案问答',
        categoriesImage:"", 
        showerror:"none",
        isCategoryPage:"none",
        isSearchPage:"none",
        showallDisplay: "block",
        displaySwiper: "block",
        floatDisplay: "none",
        searchKey:"",
        description:'',
        tabs: [{id:0,sulg:'',name:'全部'},{id:0,sulg:'dacx',name:'档案查询'}, {id:0,sulg:'dabb',name:'补档知识'}, {id:0,sulg:'datg',name:'档案托管'}, {id:0,sulg:'bdz', name:'报到证'}],
        name:'',
        stv: {
            windowWidth: 0,
            lineWidth: 0,
            offset: 0,
            tStart: false
        },
        activeTab: 0,
        picnewslist: [],
        sulg:''
    },
    //加载分页
  loadMore: function (e) {
    var self = this;
    if (!self.data.isLastPage) {
      self.setData({
        page: self.data.page + 1,

      });
      console.log('当前页' + self.data.page);
      this.fetchPostsData(self.data);
    }
    else {
      swan.showToast({
        title: '没有更多内容',
        mask: false,
        duration: 1000
      });
    }
  },
  onLoad: function (options) {
    var self = this;
     // 监听页面加载的生命周期函数
        try {
            let { tabs } = this.data;
            // var res = swan.getSystemInfoSync()
            this.windowWidth = 670;
            this.data.stv.lineWidth = this.windowWidth / this.data.tabs.length;
            this.data.stv.windowWidth = this.windowWidth;
            this.setData({ stv: this.data.stv })
            this.tabsCount = tabs.length;
           
        } catch (e) {
        }
        this.fetchPostsData();
        this.setConfig(seoConfig.indexConfig)
        
        
  },
  setConfig(seo) {
        swan.setPageInfo && swan.setPageInfo(seo)
    },
  //获取文章列表数据
  fetchPostsData: function (data) {
    var self = this;  
    if (!data) data = {};
    if (!data.page) data.page = 1;
    if (!data.sulg) data.sulg = '';
    if (!data.search) data.search = '';
    if (data.page === 1) {
      self.setData({
        postsList: []
      });
    };
    
    swan.showLoading({
      title: '正在加载',
      mask:true
    });

    var getPostsRequest = bdRequest.getRequest(Api.getQapost(data));
    getPostsRequest.then(response =>{
        if (response.statusCode === 200) {
            self.setData({
                floatDisplay: "block",
                showallDisplay: "block",
                postsList: self.data.postsList.concat(response.data.data.map(function (item) {         
                    var strdate = item.post_date
                    item.date = util.cutstr(strdate, 10, 1);
                   
                    return item;
                })),

            });
            
             if (response.data.data.length < 10) {
                self.setData({
                    isLastPage: true
                });

            }
            else {
               self.setData({
                    isLastPage: false
                });
            }
         }
        
    })
    .catch(function(){        
        // if (data.page == 1) {
        //     self.setData({
        //         showerror: "block",
        //         floatDisplay: "none"
        //     });

        // }
        // else {
        //     swan.showModal({
        //         title: '加载失败',
        //         content: '加载数据失败,请重试.',
        //         showCancel: false,
        //     });
        //     self.setData({
        //         page: data.page - 1
        //     });
        // }
    })
        .finally(function () {
            swan.hideLoading();

        })  
  },  
   _updateSelectedPage(page) {
        let { tabs, stv, activeTab } = this.data;
        activeTab = page;
        this.setData({ activeTab: activeTab })
        stv.offset = stv.windowWidth * activeTab;
        this.setData({ stv: this.data.stv })
    },
    handlerTabTap(e) {
        this._updateSelectedPage(e.currentTarget.dataset.index);
         
        this.setData({
            page:1,
            sulg:e.currentTarget.dataset.sulg
            
        })
        this.fetchPostsData({page:1,
            sulg:e.currentTarget.dataset.sulg});
    },
  // 跳转至查看文章详情
  redictDetail: function (e) {
    // console.log('查看文章');
    var id = e.currentTarget.id,
      url = '../detail/detail?id=' + id;
    swan.navigateTo({
      url: url
    })
  },

  //获取分类列表
//   fetchSpecialData: function (id) {
//     var self = this;
//     self.setData({
//       categoriesList: []
//     });

//     var getCategoryRequest = bdRequest.getRequest(Api.getSpecial(100));

//     getCategoryRequest.then(response =>{

//         var catImage = "";
//         if (typeof (response.data.category_thumbnail_image) == "undefined" || response.data.category_thumbnail_image == "") {
//             catImage = "https://www.bds110.com/wp-content/uploads/bdsmp-dacx/website.png";
//         }
//         else {
//             catImage = response.data.category_thumbnail_image;
//         }

//         self.setData({
//             categoriesList: response.data,
//             categoriesImage: catImage,
//             categoriesName: response.data.name,
//              description: response.data.description,

//         });

//         swan.setNavigationBarTitle({
//             title: response.data.name,
//             success: function (res) {
//                 // success
//             }
//         });

//         self.fetchPostsData(self.data); 

//     })
//   },
   })
